// ****************************************************************************** 
// ** ETH Zurich 2010 // Romain BEKER, Conrado PLANO, Pierre-Olivier TRABICHET ** 
// ****************************************************************************** 

// function to test if an object is contained inside an array
// arguments :
//		** a : an array
//		** o : an object
// return "true" or "false"
function contains(a,o) 
{
  var i = a.length;
  while (i--) 
  {
    if (a[i] === o) 
	{
      return true;
    }
  }
  return false;
}

// function to test if the positions chosen for a boat are valid and available
// arguments :
//		** used: array containing positions already used by other boats
//		** start : start position of the new boat
//		** size : size of the new boat
//		** d : direction of the new boat (d = 0 ==> vertical, d = 1 ==> horizontal) 
// return "true" or "false"
function testboat(used,start,size,d)
{
	if (d === 0)
	{
		// test if the chosen positions are valid
		if ((Math.floor(start/10)) < (Math.floor((start+size-1)/10)))
		{
			return false;
		}
		
		// test if the chosen positions are already used
		for (i=0; i <size; i++)
		{
			if (contains(used,start+i))
			{
				return false;
			}
		}
	}
	else
	{
		// test if the chosen positions are valid
		if ((start+((size-1)*10)) > 99)
		{
			return false;
		}
		
		// test if the chosen positions are already used
		for (i=0; i <size; i++)
		{
			if (contains(used,start+i*10))
			{
				return false;
			}
		}
	}
	
	// the positions are valid
	return true;	
}

// function to randomly place a new boat
// arguments :
//		** used : array containing positions already used by other boats
//		** size : size of the new boat
// return an array containing the positions used by the new boat
function createboat(used,size)
{
	var boat = new Array(size);
	var ok = false;
	var direction;
	var p1;

	while(!ok)
	{
		// find a random start position and a random direction for the boat
		direction=Math.floor(Math.random()*2);
		p1=Math.floor(Math.random()*99);
		
		// test if the chosen position and direction are usable
		ok = testboat(used,p1,size,direction);
	}
	
	// calculate the positions used by the new boat
	for (i=0; i <size; i++)
	{
		boat[i] = p1+i+direction*9*i;
	}
	
	return boat;
}


// function to randomly generate all the necessary boats for a battleship game
// return an array containing the positions used by all the boats
function createboats() 
{
	var boats=new Array();
	
	//generate one 2-boat
	boats = boats.concat(createboat(boats,2));	
	
	//generate one 3-boat
	boats = boats.concat(createboat(boats,3));	
	
	//generate one 3-boat
	boats = boats.concat(createboat(boats,3));	
	
	//generate one 4-boat
	boats = boats.concat(createboat(boats,4));	

	//generate one 5-boat
	boats = boats.concat(createboat(boats,5));		
	
	return boats;
}

// function to generate player's playboard
// return an array composed of 17 "boat" and 83 "sea"  
function createmytable()
{
	var my=new Array(100);
	
	var b=new Array(17);
	b = createboats();
	
	for (i=0; i <100; i++)
	{
		my[i] = "sea";
	}
	for (i=0; i <17; i++)
	{
		my[b[i]] = "boat";
	}
	
	return my;	
}

// function to generate adversary's playboard
// return an array full of "hidden"
function createhistable()
{
	var his=new Array();
	
	for (i=0; i <100; i++)
	{
		his[i] = "hidden";
	}
	
	return his;	
}